from app.docker.router import docker
from plugin.dk.Container import Container
from flask import jsonify
from plugin.utils.request import input
from plugin.dk.Network import NetWork

container = Container()
network = NetWork()


@docker.route('container/list')
def getList():
    result = container.list(True)
    data = {}
    data['code'] = 200
    data['data'] = result
    return jsonify(data), data['code']


@docker.route('container/stop', methods=['POST', 'DELETE', 'PUT'])
def stop():
    data = {}
    try:
        for containerId in input('container'):
            # 一个字典循环操作
            container.stop(containerId)
        data['code'] = 200
        data['message'] = 'success'
    except Exception as e:
        data['code'] = 500
        data['message'] = str(e)
    return jsonify(data), data['code']


@docker.route('container/start', methods=['POST', 'DELETE', 'PUT'])
def start():
    data = {}
    try:
        for containerId in input('container'):
            # 一个字典循环操作
            container.start(containerId)
        data['code'] = 200
        data['message'] = 'success'
    except Exception as e:
        data['code'] = 500
        data['message'] = str(e)
    return jsonify(data), data['code']


@docker.route('container/restart', methods=['POST', 'PUT'])
def restart():
    data = {}
    try:
        for containerId in input('container'):
            # 一个字典循环操作
            container.restart(containerId)
        data['code'] = 200
        data['message'] = 'success'
    except Exception as e:
        data['code'] = 500
        data['message'] = str(e)
    return jsonify(data), data['code']


@docker.route('container/remove', methods=['POST', 'DELETE'])
def delete():
    data = {}
    try:
        for containerId in input('container'):
            # 一个字典循环操作
            container.remove(containerId)
        data['code'] = 200
        data['message'] = 'success'
    except Exception as e:
        data['code'] = 500
        data['message'] = str(e)
    return jsonify(data), data['code']


@docker.route('container/create', methods=['POST', 'GET'])
def create():
    from ..common.container import getEnvList
    from ..common.container import getPortGroup
    from ..common.container import assignIP
    from ..common.container import getInputContainerNetwork
    from ..common.container import getNetWorkIPConfig
    from ..common.image import downloadImage
    data = {}
    try:
        networkdict = input('network')
        args = getInputContainerNetwork(networkdict)
        args = {} if args is None else args
        args['name'] = input('name')
        image = input('image')
        result = []
        if image == 'dockerhub':
            result = downloadImage(input('url'))
            image = result.attrs['RepoTags'][0]

        args['ports'] = getPortGroup(input('port'))
        args['tty'] = input('tty')
        args['environment'] = getEnvList(input('env'))
        containerObj = container.create(image, None, **args)
        ipconfig = getNetWorkIPConfig(networkdict)

        containersss = containerObj.attrs
        if networkdict is not None:
            if len(ipconfig) > 0 and networkdict['network_mode'] is not None:
                assignIP(containersss, **ipconfig)
        data['code'] = 200
        data['message'] = 'success'
    except Exception as e:
        data['message'] = str(e)
        data['code'] = 500
    return jsonify(data), data['code']
